package cn.kgc.vue.service.impl;

import cn.kgc.vue.commons.ResponseResult;
import cn.kgc.vue.entity.User;
import cn.kgc.vue.mapper.UserMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.kgc.vue.entity.Dept;
import cn.kgc.vue.service.DeptService;
import cn.kgc.vue.mapper.DeptMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author mengshun
* @description 针对表【sys_dept】的数据库操作Service实现
* @createDate 2024-04-11 10:43:41
*/
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept>  implements DeptService{


    @Resource
    private UserMapper userMapper;
    @Override
    public ResponseResult deleteDept(Integer id) {

        // 判定 部门是否还有员工
        LambdaQueryWrapper<User> lambda = new QueryWrapper<User>().lambda();
        lambda.eq(User::getDeptId,id);

        List<User> users = userMapper.selectList(lambda);

        if (ObjectUtils.isNotNull(users)){
            return  ResponseResult.fail().message("当前部门有员工,不能删除");
        }

        baseMapper.deleteById(id);

        return ResponseResult.success().message("部门删除成功");
    }
}




